home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / graphics 2d / out of this gworld / draw.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  5.9 KB  |  302 lines

  1. /*
  2.     File:        draw.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Edgar Lee    
  7.  
  8.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/16/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23. #include "out.h"
  24.  
  25. void shadeSCurve(int x, int y, int radout, int radin, int index);
  26. void drawColorSpheres(void);
  27.  
  28. void createColorScale()
  29. {
  30.     int i;
  31.     int x;
  32.     int down = true;
  33.     int    index = 8;
  34.  
  35.     PenSize( 14, 1 );
  36.     
  37.     for (x = -420; x < 420; x += 105)
  38.     {
  39.         for (i = 0; i < 30; i++)
  40.         {
  41.             setColor( index );
  42.             
  43.             if (down == true)
  44.                 drawLine( x, 420 - (i * 28) , x, 420 - ((i + 1) * 28) );
  45.             else
  46.                 drawLine( x, -420 + (i * 28) , x, -420 + ((i + 1) * 28) );
  47.             index++;
  48.         }
  49.         
  50.         down = 1 - down;
  51.     }
  52.     
  53.     down = false;
  54.     index = 8;
  55.  
  56.     for (x = 368; x > -472; x -= 105)
  57.     {
  58.         for (i = 0; i < 30; i++)
  59.         {
  60.             setColor( index );
  61.             
  62.             if (down == true)
  63.                 drawLine( x, 420 - (i * 28) , x, 420 - ((i + 1) * 28) );
  64.             else
  65.                 drawLine( x, -420 + (i * 28) , x, -420 + ((i + 1) * 28) );
  66.             index++;
  67.         }
  68.         
  69.         down = 1 - down;
  70.     }
  71. }
  72.  
  73. void createColorWheels()
  74. {
  75.     testTriangle( -400, 300 );
  76.     testTriangle( 400, 300 );
  77.     testTriangle( 400, -300 );
  78.     testTriangle( -400, -300 );
  79.     shadeWasher( 0, 0, 360, 80 );
  80. }
  81.  
  82. void createColorRings()
  83. {
  84.     shadeWasher( 0, 0, 500, 400 );
  85.     shadeWasher( 0, 0, 400, 300 );
  86.     shadeWasher( 0, 0, 300, 200 );
  87.     shadeWasher( 0, 0, 200, 100 );
  88. }
  89.  
  90. void createColorGears()
  91. {
  92.     shadeWasher( 150, 150, 300, 100 );
  93.     shadeWasher( -150, 150, 300, 100 );
  94.     shadeWasher( -150, -150, 300, 100 );
  95.     shadeWasher( 150, -150, 300, 100 );
  96. }
  97.  
  98. void createColorCurves()
  99. {
  100.     shadeWasher( 720, 720, 900, 0 );
  101.     shadeWasher( 720, -720, 900, 0 );
  102.     shadeWasher( -720, -720, 900, 0 );
  103.     shadeWasher( -720, 720, 900, 0 );
  104.     shadeWasher( 0, 0, 400, 0 );
  105. }
  106.  
  107. void createColorBalls()
  108. {
  109.     int x, y;
  110.  
  111.     for (x = -432; x <= 432; x += 108)
  112.         for (y = -432; y <= 432; y += 108)
  113.             shadeWasher( x, y, 60, 0 );
  114. }
  115.  
  116. void createColorWave()
  117. {
  118.     shadeSCurve( -300, 250, 210, 60, 9 );
  119.     shadeSCurve( 96, 250, 210, 60, 69 );
  120.     shadeSCurve( -300, 150, 210, 60, 129 );
  121.     shadeSCurve( 96, 150, 210, 60, 189 );
  122.     shadeSCurve( -300, 50, 210, 60, 9 );
  123.     shadeSCurve( 96, 50, 210, 60, 69 );
  124.     shadeSCurve( -300, -50, 210, 60, 129 );
  125.     shadeSCurve( 96, -50, 210, 60, 189 );
  126.     shadeSCurve( -300, -150, 210, 60, 9 );
  127.     shadeSCurve( 96, -150, 210, 60, 69 );
  128.     shadeSCurve( -300, -250, 210, 60, 129 );
  129.     shadeSCurve( 96, -250, 210, 60, 189 );
  130. }
  131.  
  132. void createColorText()
  133. {
  134.     Rect        rect;
  135.     PicHandle    thePict;
  136.     int            width, height;
  137.     
  138.     thePict = (PicHandle)GetResource( 'PICT', 129 );
  139.     
  140.     width = (**thePict).picFrame.right - (**thePict).picFrame.left;
  141.     height = (**thePict).picFrame.bottom - (**thePict).picFrame.top;
  142.  
  143.     SetRect( &rect, (WWIDTH - width) / 2, (WHEIGHT - height) / 2, 
  144.                     ((WWIDTH - width) / 2) + width, ((WHEIGHT - height) / 2 ) + height );
  145.  
  146.     DrawPicture( thePict, &rect );
  147.     ReleaseResource( (Handle)thePict );
  148. }
  149.  
  150. void drawColorSpheres()
  151. {
  152.     int rad;
  153.     int i;
  154.  
  155.     for (i = 9; i < 48; i++)
  156.     {
  157.         setColor( i );
  158.         rad = (48 - i) * 3;
  159.         drawCircle( (int)(i * 1.414), (int)(i * 1.414), rad );
  160.     }
  161. }
  162.  
  163. void testTriangle( x, y )
  164. int x, y;
  165. {
  166.     PenSize( 1, 1 );
  167.  
  168.     setColor( 7 );
  169.     drawCircle( x, y, 25 );
  170.  
  171.     setColor( 1 );
  172.     drawWasher( x, y, 25, 60, -30, 30 );
  173.     drawLine( x - 60, y + 40, x, y + 140 );
  174.     drawLine( x, y + 140, x + 60, y + 40 );
  175.     setColor( 2 );
  176.     drawWasher(x, y, 25, 60, 30, 90 );
  177.     setColor( 3 );
  178.     drawWasher(x, y, 25, 60, 90, 150 );
  179.     drawLine( x + 60, y + 40, x + 121, y - 70 );
  180.     drawLine( x + 121, y - 70, x, y - 70 );
  181.     setColor( 4 );
  182.     drawWasher( x, y, 25, 60, 150, 210 );
  183.     setColor( 5 );
  184.     drawWasher( x, y, 25, 60, 210, 270 );
  185.     drawLine( x - 60, y + 40, x - 121, y - 70 );
  186.     drawLine( x - 121, y - 70, x, y - 70 );
  187.     setColor( 6 );
  188.     drawWasher( x, y, 25, 60, 270, 330 );
  189. }
  190.  
  191. void shadeWasher( x, y, radout, radin )
  192. int x, y;
  193. int radout, radin;
  194. {
  195.     int ang;
  196.     int i = 8;
  197.     int radmid;
  198.  
  199.     radmid = radin + (radout - radin) / 2;
  200.     
  201.     for (ang = 0; ang < 360; ang += 3)
  202.     {
  203.         setColor( i );
  204.         drawWasher( x, y, radmid, radout, ang, ang + 3 );
  205.         i++;
  206.     }
  207.     
  208.     for (ang = 0; ang < 360; ang += 3)
  209.     {
  210.         setColor( i );
  211.         drawWasher( x, y, radin, radmid, ang, ang + 3 );
  212.         i++;
  213.     }
  214. }
  215.  
  216. void shadeSCurve( x, y, radout, radin, index)
  217. int x, y;
  218. int radout, radin;
  219. int index;
  220. {
  221. #pragma unused (radout)
  222. #pragma unused (radin)
  223.  
  224.     int ang;
  225.     
  226.     for (ang = 270; ang > 90; ang -= 6)
  227.     {
  228.         setColor( index );
  229.         drawWasher( x, y, 60, 210, ang, ang - 6 );
  230.         index++;
  231.     }
  232.  
  233.     for (ang = -90; ang < 90; ang += 6)
  234.     {
  235.         setColor( index );
  236.         drawWasher( x + 198, y, 60, 210, ang, ang + 6 );
  237.         index++;
  238.     }
  239. }
  240.  
  241. void translate( x, y )
  242. int *x, *y;
  243. {
  244.     int xx, yy;
  245.     
  246.     xx = *x / SCALE;
  247.     yy = *y / SCALE;
  248.     
  249.     *x = xx + (WWIDTH / 2);
  250.     *y = (-yy) + (WHEIGHT / 2);
  251. }
  252.  
  253. void scale( len )
  254. int *len;
  255. {
  256.     *len /= SCALE;
  257. }
  258.  
  259. void drawCircle( x, y, radius )
  260. int x, y;
  261. int radius;
  262. {
  263.     Rect     rect;
  264.     int        foo = 0;
  265.     
  266.     translate( &x, &y );
  267.     scale( &radius );
  268.  
  269.     SetRect( &rect, x - radius, y - radius, x + radius, y + radius );
  270.     PaintOval( &rect );
  271. }
  272.  
  273. void drawWasher( x, y, irad, orad, sang, eang )
  274. int x, y;
  275. int irad, orad;
  276. int sang, eang;
  277. {
  278.     int        width;
  279.     Rect    rect;
  280.     
  281.     translate( &x, &y );
  282.     scale( &irad );
  283.     scale( &orad );
  284.     
  285.     width = (orad - irad) / 2;
  286.     
  287.     PenSize( width, width );
  288.     SetRect( &rect, x - (irad + width), y - (irad + width),
  289.                     x + (irad + width), y + (irad + width) );
  290.                     
  291.     FrameArc( &rect, sang, eang - sang );
  292.     PenSize( 1, 1 );
  293. }
  294.  
  295. void drawLine( sx, sy, ex, ey )
  296. {    
  297.     translate( &sx, &sy );
  298.     translate( &ex, &ey );
  299.     
  300.     MoveTo( sx, sy );
  301.     LineTo( ex, ey );
  302. }